<template>
  <div>
    <!-- 总部库存汇总查询 -->
    <TopFilter :list="topFilterList" :cols="3" @filterChange="changeHandle" @onCollapse="collapseHandle" :labelWidth="110"></TopFilter>

    <Spin :spinning="exportLoading" tip="Loading...">
      <FilterTable
        ref="table"
        columnsRef="zjAggregateQuery"
        :isSelectColumn="false"
        :columns="columns"
        :fetchapi="fetchApi"
        :params="fetchParams"
        :datakey="'records'"
        :onColumnsChange="columns => (this.columns = columns)"
      >
        <template slot="controls">
          <!-- <el-button size="small" type="text" style="font-size:14px; font-weight:bold;margin-right:20px;">
            <span v-text="`总部库存数合计:${NumberData.totalSumTotal || 0}台,`"></span>
            <span v-text="`订单占用数量合计: ${NumberData.orderSumTotal || 0}台,`"></span>
            <span v-text="`锁定数量合计: ${NumberData.lockSumTotal || 0}台,`"></span>
            <span v-text="`可用数量合计: ${NumberData.availableSumTotal || 0}台`"></span>
          </el-button> -->
          <multiuse-button size="small" type="primary" :click="exportHandle">导 出</multiuse-button>
        </template>
      </FilterTable>
    </Spin>
  </div>
</template>
<script>
import { dictionary } from '@/utils/dictMixin';
import { queryList, vehicleConfig, getFactoryWarehouseList, getWarehouseList, exportExcel, getWarehousing,exporturl,getpackageName } from '@/api/omSale/HeadquartersInventory/AggregateQuery';
import { exportExcelFile } from '@/utils/index';

export default {
  name: 'AggregateQuery',
  mixins: [dictionary],

  data() {
    this.BaseTable = null;
    return {
      topFilterList: this.createdFrom(),
      columns: this.createdTable(),
      fetchApi: queryList,
      fetchParams: {
        xhrAbort: true
      },
      exportLoading: false,
      NumberData: {
        totalSumTotal: 0,
        orderSumTotal: 0,
        lockSumTotal: 0,
        availableSumTotal: 0
      }
    };
  },
  components: {},
  mounted() {
    this.BaseTable = this.$refs.table.$refs.pageTable;
  },
  created() {
    this.getFactoryList();
    this.getList();
    this.vehicleInfo();
    // this.getTotal({});
  },
  methods: {
    //     // 配置名称
    // async packageName() {
    //   const res = await getpackageName();
    //   if (res.resultCode == 200) {
    //     res.data.list.map(x => {
    //       this.topFilterList.find(item => item.fieldName === 'packageName').itemList.push({ text: x.warehouseName, value: x.warehouseId });
    //     });
    //   }
    // },
    // 工厂库
    async packageName() {
      const res = await getFactoryWarehouseList();
      if (res.resultCode == 200) {
        res.data.list.map(x => {
          this.topFilterList.find(item => item.fieldName === 'warehouseId').itemList.push({ text: x.warehouseName, value: x.warehouseId });
        });
      }
    },
    // 仓库名称
    async getList() {
      const res = await getWarehouseList();
      if (res.resultCode == 200) {
        res.data.list.map(x => {
          this.topFilterList.find(item => item.fieldName === 'subWarehouseId').itemList.push({ text: x.warehouseName, value: x.warehouseId });
        });
      }
    },
    async getTotal(data) {
      let res = await getWarehousing(data);
      this.NumberData = {
        totalSumTotal: res.data.totalSumTotal || 0,
        orderSumTotal: res.data.orderSumTotal || 0,
        lockSumTotal: res.data.lockSumTotal || 0,
        availableSumTotal: res.data.availableSumTotal || 0
      };
    },
    // 车型选择
    async vehicleInfo() {
      let res = await vehicleConfig(0, { isUnlimited: 10041001 });
      this.topFilterList.find(item => item.fieldName === 'vehicleInfo').itemList = res.data;
    },
    // 导出
    async exportHandle() {


      let json = Object.assign({}, { CODE: 'CODE_0000043' }, this.fetchParams);
      let res = await exporturl(json);
      if (res.resultCode == 200) {
        this.$router.push('/admin/auth/oem/downloadCenter');
      }
      // await this.$confirm('是否将Excel表格保存至本地文件夹?', '提示', {
      //   confirmButtonText: '下载',
      //   cancelButtonText: '取消',
      //   type: 'warning'
      // })
      //   .then(() => {
      //     this.exportLoading = true;

      //     exportExcel(this.fetchParams)
      //       .then(response => {
      //         exportExcelFile(response.data, '总部库存汇总查询');
      //         this.exportLoading = false;
      //       })
      //       .catch(err => {
      //         console.log(err);
      //       });
      //   })
      //   .catch(() => {
      //     this.$message({
      //       type: 'info',
      //       message: '已取消'
      //     });
      //   });
    },
    changeHandle(params) {
      this.fetchParams.xhrAbort = false;

      this.getTotal(params);
      this.fetchParams = Object.assign({}, this.fetchParams, params);
    },
    collapseHandle() {
      this.$nextTick(() => {
        this.BaseTable.EXECUTE_RESET_HEIGHT();
      });
    },
    createdFrom() {
      return [
        // {
        //   type: 'SELECT',
        //   label: '工厂库',
        //   fieldName: 'warehouseId',
        //   placeholder: '请选择',
        //   itemList: []
        // },
        // {
        //   type: 'SELECT',
        //   label: '工厂库',
        //   fieldName: 'subWarehouseId',
        //   placeholder: '请选择',
        //   itemList: []
        // },
        // {
        //   type: 'SELECT',
        //   label: '动力形式',
        //   fieldName: 'powerType',
        //   placeholder: '请选择',
        //   itemList: this.createDictList('6008')
        // },
        {
          type: 'INPUT',
          label: '物料描述',
          fieldName: 'productName'
        },
        // {
        //   type: 'SELECT',
        //   label: '车系名称',
        //   fieldName: 'seriesName',
        //   placeholder: '请选择',
        //   itemList: []
        // },
           {
          type: 'INPUT_CASCADER',
          label: '车系名称',
          fieldName: 'vehicleInfo',
          placeholder: '请选择',
          options: { titles: ['品牌', '车系', '车型', '配置', '选装', '颜色'] },
           //options: { titles: [ '车系', '车型', '配置', '选装', '内饰','外饰'] },
          itemList: []
        },
        {
          type: 'SELECT',
          label: '车型名称',
          fieldName: 'modelName',
          placeholder: '请选择',
          itemList: []
        },
        // {
        //   type: 'SELECT',
        //   label: '配置名称',
        //   fieldName: 'packageName',
        //   placeholder: '请选择',
        //   itemList: this.createDictList('6008')
        // },
        {
          type: 'SELECT',
          label: '配置名称',
          fieldName: 'packageName',
          placeholder: '请选择...',
          itemList: []
        },
        {
          type: 'SELECT',
          label: '选装名称',
          fieldName: 'optionName',
          placeholder: '请选择',
          itemList: this.createDictList('6008')
        },
     
        {
          type: 'SELECT',
          label: '内饰',
          fieldName: 'trimCode',
          placeholder: '请选择',
          itemList: []
        },
        {
          type: 'SELECT',
          label: '外饰',
          fieldName: 'colorCode',
          placeholder: '请选择',
          itemList: []
        },
        // {
        //   type: 'INPUT',
        //   label: '整车物料编码',
        //   fieldName: 'productCode'
        // },
        {
          type: 'INPUT',
          label: '整车物料编码',
          fieldName: 'productCode',
          placeholder: '请选择'
        },
        // {
        //   type: 'INPUT',
        //   label: '产品名称',
        //   fieldName: 'productName',
        //   placeholder: '请选择'
        // },
        // {
        //   type: 'RANGE_DATE',
        //   label: '入库日期',
        //   fieldName: 'beginVehicleCreatedAt|endVehicleCreatedAt',
        //   placeholder: '请选择',
        //   initialValue: [],
        //   dateType: 'exactdaterange'
        // },
        // {
        //   type: 'RANGE_DATE',
        //   label: '生产日期',
        //   fieldName: 'beginProductDate|endProductDate',
        //   placeholder: '请选择',
        //   initialValue: [],
        //   dateType: 'exactdaterange'
        // },
        //    {
        //   type: 'SELECT',
        //   label: '所属公司',
        //   fieldName: 'powerCompany',
        //   placeholder: '请选择',
        //   itemList: this.createDictList('6008')
        // },
      ];
    },
    createdTable() {
      return [
       {
          title: '序号',
          dataIndex: 'index',
          width: 80,
          sorter: true,
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        {
          title: '车系名称',
          dataIndex: 'seriesName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },

        {
          title: '车型名称',
          dataIndex: 'modelName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '配置名称',
          dataIndex: 'packageName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '选装名称',
          dataIndex: 'optionName',
          align: 'center',
          width: 250,
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: []
        },
        {
          title: '内饰',
          dataIndex: 'trimCode',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '外饰',
          dataIndex: 'colorCode',
          sorter: true,
          filter: true,
          filterType: 'input'
        },

        {
          title: '整车物料编码',
          dataIndex: 'productCode',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '物料描述',
          dataIndex: 'productName',
          align: 'center',
          width: 400,
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        // {
        //   title: '动力形式',
        //   dataIndex: 'powerType',
        //   sorter: true,
        //   filter: true,
        //   filterType: 'checkbox',
        //   filterItems: this.createDictList('6008'),
        //   render: props => {
        //     return <span>{this.createDictText(props.row.powerType, '6008')}</span>;
        //   }
        // },

        // {
        //   title: '整车物料编码',
        //   dataIndex: 'productCode',
        //   sorter: true,
        //   filter: true,
        //   filterType: 'input'
        // },
        // {
        //   title: '物料描述',
        //   dataIndex: 'productName',
        //   sorter: true,
        //   filter: true,
        //   filterType: 'input'
        // },
        {
          title: '总库存数',
          dataIndex: 'totalSum',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '订单占用数量',
          dataIndex: 'orderSum',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '锁定数量',
          dataIndex: 'lockSum',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '可用数量',
          dataIndex: 'availableSum',
          sorter: true,
          filter: true,
          filterType: 'input'
        }
      ];
    }
  }
};
</script>
